﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using SIS.ENT;
using SIS.BLL;
using SIS.UI;

public partial class student_student_list : System.Web.UI.Page
{
    public Common _common = new Common();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            _common.LoadSchoolDDL(ddlSchoolList, string.Empty);
            _common.LoadCountryCodeDDL(ddlCountry, string.Empty);
            //_common.LoadDistrictCodeDDL(ddlDistrict, Convert.ToInt16(ddlCountry.Text), string.Empty);

            //btnSearchStudent_Click(sender, e);
            divSearchResult.Visible = false;

            if (ddlSchoolList.Items.Count > 0)
            {
                ddlSchoolList.Text = System.Configuration.ConfigurationManager.AppSettings["SchoolKeyDefault"].ToString();

                //ddlSchoolList.SelectedIndex = 0;
                //ddlSchoolList_SelectedIndexChanged(sender, e);
            }
        }

    }
    protected void ddlSchoolList_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void grvStudentProfile_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (grvStudentProfile.SelectedIndex >= 0)
        {
            DataKey _selectedDataKey = grvStudentProfile.SelectedDataKey;
            int _studentProfileKey = Convert.ToInt32(_selectedDataKey[0]);
            int _userAccountKey = Convert.ToInt32(_selectedDataKey[1]);
            if (_studentProfileKey > 0 && _userAccountKey > 0)
            {
                lblSelectedStudentProfileKey.Text = _studentProfileKey.ToString();
                lblSelectedStudentUserAccountKey.Text = _userAccountKey.ToString();
                grvRegister.DataBind();
            }
        }
    }

    protected void btnAddNewStudent_Click(object sender, EventArgs e)
    {
        Response.Redirect("default.aspx");
    }

    protected void btnBackToSearch_Click(object sender, EventArgs e)
    {
        Response.Redirect("student-list.aspx");
    }

    protected void btnSearchStudent_Click(object sender, EventArgs e)
    {
        divSearchResult.Visible = true;

        int _schoolKey = Convert.ToInt32(ddlSchoolList.SelectedValue);
        string _Name = txtName.Text;
        if (_Name.Equals(string.Empty))
        {
            _Name = "%";
        }
        else
        {
            _Name = "%" + _Name + "%";
        }
        string _Address = txtAddress.Text;
        if (_Address.Equals(string.Empty))
        {
            _Address = "%";
        }
        else
        {
            _Address = "%" + _Address + "%";
        }
        string _NRICNO = txtNRIC.Text;
        if (_NRICNO.Equals(string.Empty))
        {
            _NRICNO = "%";
        }
        else
        {
            _NRICNO = "%" + _NRICNO + "%";
        }
        string _AddressType = ddlAddressType.SelectedValue;
        string _CountryCode = ddlCountry.SelectedValue;
        if (_CountryCode.Equals("-1"))
        {
            _CountryCode = "-1";
        }

        string _CityCode = ddlCity.SelectedValue;
        if (_CityCode.Equals("-1"))
        {
            _CityCode = "-1";
        }

        string _DistrictCode = ddlDistrict.SelectedValue;
        if (_DistrictCode.Equals("-1"))
        {
            _DistrictCode = "-1";
        }


        StudentProfileTableList lList = new StudentProfileTableList();
        lList = StudentProfileTableManager.GetList(_schoolKey, _Name, _Address, _NRICNO, _AddressType, _CountryCode, _CityCode, _DistrictCode);
        grvStudentProfile.DataSource = lList;
        grvStudentProfile.DataBind();

        if (grvStudentProfile.Rows.Count > 0)
        {
            grvStudentProfile.SelectedIndex = 0;
            grvStudentProfile_SelectedIndexChanged(sender, e);

        }
        divSearch.Visible = false;
    }

    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        _common.LoadDistrictCodeDDL(ddlDistrict, Convert.ToInt16(ddlCountry.Text), string.Empty);
        ddlDistrict_SelectedIndexChanged(sender, e);

    }
    protected void ddlDistrict_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlDistrict.SelectedIndex >= 0)
        {
            _common.LoadCityCodeDDL(ddlCity, Convert.ToInt32(ddlDistrict.Text), Convert.ToInt16(ddlCountry.Text), string.Empty);
            ddlCity.Items.Insert(0, new ListItem("ALL", "-1"));
        }
    }
}
